// pages/userinfo/userinfo.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tanchuangsex:false,
    userinfo:{},
    touxiang:"",
    nickname:"",
  },
  selectsex:function(e){
    var index = e.currentTarget.dataset.index;
    var str ="userinfo.Sex"
    this.setData({
      [str]:index
    })
    this.updatasex()
  },
  tobaocun: function () {
    ajax('/api/PersonDetails/Update?token=' + wx.getStorageSync('Access_token') + "&Sex=" + this.data.userinfo.Sex,
      {
        header: {
          'Accept': 'multipart/form-data', // 默认值
        },
        method: "post",
      }, res => {
        console.log(res)
        if (res.Status==1){
          wx.reLaunch({
            url: '/pages/mine/mine',
          })
        }
        
      })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    if (options.nickname){
      var nickname = options.nickname
      this.setData({
        nickname: nickname
      })
    }
  },
  updatasex:function(){
    this.setData({
      tanchuangsex: !this.data.tanchuangsex
    })
  },  
  toupdatanickname:function(){
    console.log("s")
    wx.navigateTo({
      url: '/pages/updatanickname/updatanickname',
    })
  },

  uploadMedical: function () {
    var that=this
    wx.showActionSheet({
      itemList: ['从相册选取', '拍照'],
      success(res) {
        var index = res.tapIndex;
        var dogphoto = that.data.dogphoto;
        if (index == 0) {
          let num = 1; //一次最大上传九张
          wx.chooseImage({
            count: num,
            sizeType: ['original', 'compressed'],
            sourceType: ['album'],
            success(res) {
              console.log(res)
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              console.log(tempFilePaths)
              var touxiang = tempFilePaths[0]
              
              that.setData({
                touxiang: touxiang
              });
              that.shangchuantp()
             
            }
          })
        } else if (index == 1) {
          wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['camera'],
            success(res) {
              console.log(res)
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              var touxiang = tempFilePaths[0]

              that.setData({
                touxiang: touxiang
              });
              that.shangchuantp()
            }
          })
        }

      },
      fail(res) {
        console.log(res.errMsg)
      }
    })

  },

  shangchuantp:function(){
    wx.uploadFile({
      url: 'https://xfz.897xls.cn/api/PersonDetails/Update?', //仅为示例，非真实的接口地址
      filePath: this.data.touxiang,
      name: 'PersonImg',
      formData: {
        // PersonImg: this.data.touxiang,
        'token': wx.getStorageSync('Access_token'),
      },
      success(res) {
        const data = res.data
        //do something
      }
    })
  },


  getpersondetails: function () {
    ajax('/api/PersonDetails/GetInfo?requestContent=' + JSON.stringify({
      token: wx.getStorageSync("Access_token")
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        console.log(res)
        this.setData({
          userinfo: res.Models[0]
        })

      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getpersondetails()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})